<!-- src/views/outbound/bbb/bbb.vue -->
<script setup>
import { ref, onMounted } from 'vue';
import { getOutboundOrderPageByStatusApi } from '@/api/outbound/outboundOrder';
import { getUserListApi } from '@/api/user.js';
import { ElMessage } from 'element-plus';

// 表格数据相关
const tableData = ref([]);
const loading = ref(false);
const pagination = ref({
  page: 1,
  pageSize: 10,
  total: 0
});

// 搜索表单
const searchForm = ref({
  invoiceCode: ''
});

// 客户列表数据
const customerList = ref([]);

// 状态选项
const statusOptions = [
  { label: '预发货', value: 1 },
  { label: '新发货', value: 2 },
  { label: '待拣货', value: 3 },
  { label: '已拣货', value: 4 },
  { label: '打包', value: 5 },
  { label: '称重', value: 6 },
  { label: '出库', value: 7 },
  { label: '已签收', value: 8 },
];

// 查询出库订单列表（按状态）
const fetchOutboundOrderList = async () => {
  loading.value = true;
  try {
    // 调用按状态查询的接口，status固定为1
    const res = await getOutboundOrderPageByStatusApi(
      pagination.value.page,
      pagination.value.pageSize,
      2, // status固定为1
      searchForm.value.invoiceCode // 传入发货单号搜索条件
    );
    tableData.value = res.data.rows;
    pagination.value.total = res.data.total;
  } catch (error) {
    ElMessage.error('获取出库订单列表失败');
  } finally {
    loading.value = false;
  }
};

// 获取客户列表
const fetchCustomerList = async () => {
  try {
    const res = await getUserListApi();
    customerList.value = res.data;
  } catch (error) {
    ElMessage.error('获取客户列表失败');
  }
};

// 根据客户ID获取客户名称
const getCustomerName = (customerId) => {
  const customer = customerList.value.find(item => item.id == customerId);
  return customer ? customer.name : '未知客户';
};

// 获取状态标签
const getStatusLabel = (status) => {
  const statusMap = {
    1: '预发货',
    2: '新发货',
    3: '待拣货',
    4: '已拣货',
    5: '打包',
    6: '称重',
    7: '出库',
    8: '已签收'
  };
  return statusMap[status] || '未知状态';
};

// 分页处理
const handlePageChange = (page) => {
  pagination.value.page = page;
  fetchOutboundOrderList();
};

// 搜索处理
const handleSearch = () => {
  pagination.value.page = 1;
  fetchOutboundOrderList();
};

// 重置搜索
const resetSearch = () => {
  searchForm.value.invoiceCode = '';
  pagination.value.page = 1;
  fetchOutboundOrderList();
};

onMounted(() => {
  fetchOutboundOrderList();
  fetchCustomerList();
});
</script>

<template>
  <div class="outbound-order-container">

    <!-- 搜索区域 -->
    <div class="toolbar">
      <div class="toolbar-right">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item label="发货单号">
            <el-input 
              v-model="searchForm.invoiceCode" 
              placeholder="请输入发货单号" 
              clearable 
              style="width: 200px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleSearch">查询</el-button>
            <el-button icon="Refresh" @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 出库订单表格 -->
    <el-table 
      :data="tableData" 
      v-loading="loading" 
      border 
      style="width: 100%; margin-top: 20px;"
    >
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="invoiceCode" label="发货单号" min-width="180" />
      <el-table-column prop="customerId" label="客户" width="120">
        <template #default="scope">
          {{ getCustomerName(scope.row.customerId) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-form :type="scope.row.status === 1 ? 'primary' : scope.row.status === 8 ? 'success' : 'info'">
            {{ getStatusLabel(scope.row.status) }}
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="totalCount" label="总数量" width="100" />
      <el-table-column prop="totalWeight" label="总重量" width="100" />
      <el-table-column prop="totalVolume" label="总体积" width="100" />
      <el-table-column prop="createTime" label="创建时间" min-width="160" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="scope">
          <el-button size="small" type="primary" link>详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      layout="total, prev, pager, next, jumper"
      :current-page="pagination.page"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="handlePageChange"
      style="margin-top: 20px; text-align: right;"
    />
  </div>
</template>

<style scoped>
.outbound-order-container {
  padding: 20px;
  background-color: #fff;
}

.header {
  margin-bottom: 20px;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;
}

.toolbar-left {
  flex-shrink: 0;
}

.toolbar-right {
  flex: 1;
  min-width: 0;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

@media (max-width: 768px) {
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  
  .toolbar-left {
    margin-bottom: 10px;
  }
  
  .toolbar-right {
    display: flex;
    justify-content: center;
  }
  
  .search-form {
    justify-content: center;
  }
}
</style>